package com.xckj.teacher.presenter;

import androidx.annotation.NonNull;

import com.xckj.teacher.bean.BaseBean;
import com.xckj.teacher.bean.HomeBean;
import com.xckj.teacher.bean.TextbookBean;
import com.xckj.teacher.bean.UnitBean;
import com.xckj.teacher.framework.net.RetrofitManager;
import com.xckj.teacher.global.Constant;
import com.xckj.teacher.iview.IHomeView;

import java.util.List;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: ChenYao
 * @Describe: 首页
 * @Date: 2024/4/11 15:14
 */
public class HomePresenter {
    private final IHomeView homeView;

    public HomePresenter(IHomeView homeView) {
        this.homeView = homeView;
    }

    /**
     * 获取首页数据
     */
    public void getHome() {
        homeView.showLoading();
        Call<BaseBean<HomeBean>> call = RetrofitManager.getInstance().getService().getHome();
        call.enqueue(new Callback<BaseBean<HomeBean>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<HomeBean>> call, @NonNull Response<BaseBean<HomeBean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<HomeBean> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        homeView.success(baseBean.data);
                    } else {
                        homeView.failed(baseBean.msg);
                    }
                } else {
                    homeView.failed("请求失败");
                }
                homeView.hideLoading();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<HomeBean>> call, @NonNull Throwable t) {
                t.printStackTrace();
                homeView.failed("请求失败");
                homeView.hideLoading();
            }
        });
    }

    /**
     * 获取教材
     */
    public void getTextbook() {
        Call<BaseBean<List<TextbookBean>>> call = RetrofitManager.getInstance().getService().getTextbook();
        call.enqueue(new Callback<BaseBean<List<TextbookBean>>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<List<TextbookBean>>> call, @NonNull Response<BaseBean<List<TextbookBean>>> response) {
                if (response.isSuccessful()) {
                    BaseBean<List<TextbookBean>> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        homeView.success(baseBean.data);
                    } else {
                        homeView.failed(baseBean.msg);
                    }
                } else {
                    homeView.failed("请求失败");
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<List<TextbookBean>>> call, @NonNull Throwable t) {
                t.printStackTrace();
                homeView.failed("请求失败");
            }
        });

    }

    /**
     * 获取单元
     *
     * @param id 版本Id
     */
    public void getUnit(String id) {
        Call<BaseBean<List<UnitBean>>> call = RetrofitManager.getInstance().getService().unit(id);
        call.enqueue(new Callback<BaseBean<List<UnitBean>>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<List<UnitBean>>> call, @NonNull Response<BaseBean<List<UnitBean>>> response) {
                if (response.isSuccessful()) {
                    BaseBean<List<UnitBean>> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        homeView.successUnit(baseBean.data);
                    } else {
                        homeView.failed(baseBean.msg);
                    }
                } else {
                    homeView.failed("请求失败");
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<List<UnitBean>>> call, @NonNull Throwable t) {
                t.printStackTrace();
                homeView.failed("请求失败");
            }
        });
    }

    /**
     * 保存单元
     *
     * @param unitId    单元Id
     * @param versionId 版本Id
     */
    public void saveUnit(String unitId, String versionId) {
        Call<BaseBean> call = RetrofitManager.getInstance().getService().saveUnit(unitId, versionId);
        call.enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean> call, @NonNull Response<BaseBean> response) {
                if (response.isSuccessful()) {
                    BaseBean  baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        homeView.success();
                    } else {
                        homeView.failed(baseBean.msg);
                    }
                } else {
                    homeView.failed("请求失败");
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean> call, @NonNull Throwable t) {
                t.printStackTrace();
                homeView.failed("请求失败");
            }
        });
    }
}
